package com.ruoyi.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class CxBase extends BaseController {
    /** 检测是否为空
     *
     * return false 不为空
     * return true   为空
     * */
    protected Boolean checkNull(Object i)
    {
        if(i!=null)
        {
            i = i.toString();
            if(!i.equals(""))
                return false;
            else
                return true;
        }
        else{
            return true;
        }
    }



    /**
     * 设置请求分页数据
     */
    protected void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
        {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        if(list!=null) {
            rspData.setCode(HttpStatus.SUCCESS);
            rspData.setMsg("查询成功");
            rspData.setRows(list);
            rspData.setTotal(new PageInfo(list).getTotal());
            return rspData;
        }
        else{
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("查询失败");
            return rspData;
        }
    }

    protected TableDataInfo getDataTable(int code,String msg)
    {
        TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(code);
            rspData.setMsg(msg);
            return rspData;
    }

    protected TableDataInfo getDataTable(Object list)
    {
        TableDataInfo rspData = new TableDataInfo();
        List<Object> list1 = new ArrayList<>();
        list1.add(list);
        if(list!=null) {
            rspData.setCode(HttpStatus.SUCCESS);
            rspData.setMsg("查询成功");
            rspData.setRows(list1);
            rspData.setTotal(list1.size());
            return rspData;
        }
        else{
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("查询失败");
            return rspData;
        }
    }

    /**六位随机数*/
    private String randomran(){
        Random randomseed = new Random();
        Random random = new Random(randomseed.nextInt());
        int randomInt;
        String randomString="";
        for(int i=0;i<6;i++) {
            randomInt = random.nextInt(10);
            randomString += String.valueOf(randomInt);
        }
        return randomString;
    }

    /** 获取系统当前时间 */
    protected Date getlocaltime()
    {
        return new Date();
    }
    /** 转换系统当前时间 */
    protected String getStringtime(Date date)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(date);
    }

        /** 获得当前Id时间戳 */
        protected Long getIdStamp(){
            Date date = new Date();
            Long time = date.getTime()/1000;
            time = Long.valueOf(time.toString()+randomran());
            return time;
        }

        /** 获得当前时间戳 */
        protected Long getStamp(){
            Date date = new Date();
            Long time = date.getTime()/1000;
            return time;
        }

    /** 转换为时间戳 */
        protected Long dateToStamp(Date datestamp){
            Long time = datestamp.getTime()/1000;
            return time;
        }

        /** 转换为日期String */
        protected String StampToString(long stamp){
            if(stamp>0) {
                if(stamp>10000000000l){
                    stamp = stamp/1000000;
                }
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(stamp * 1000));
            }
            else{
                return "无";
            }
        }

        /** 转换为日期 */
        protected Date StampToDate(long stamp){
            if(stamp>10000000000l){
                stamp = stamp/1000000;
            }
            stamp = stamp/1000000;
            Date res = new Date(stamp*1000);
            return res;
        }

        public static String getBase64(String str) {
            byte[] b = null;
            String s = null;
            try {
                b = str.getBytes("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            if (b != null) {
                s = new BASE64Encoder().encode(b);
            }
            return s;
        }
}
